package com.cai.piao.project

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn_left.setOnClickListener {
            cofoxShuangSeQiu(f_number)
        }
        btn_right.setOnClickListener {
            cofoxDaLeTou(f_number)
        }
    }
    /**
     * 取得min到max之间的随机整数（不包含max）
     * @min 最小取值
     * @max 最大取值边界（此值不取）
     * */
    fun cofoxRandom(min: Int = 0, max: Int): Int {
        var t_min = min
        var t_max = max
        if (t_min > t_max) {
            var temp: Int
            temp = t_min
            t_min = t_max
            t_max = temp
        }
        var random = (Math.floor(Math.random() * (t_max - t_min))).toInt() + t_min

        return random
    }
    /**
     * 福彩双色球随机猜想出号
     * */
    fun cofoxShuangSeQiu(ttvw: TextView){
//    var ints = intArrayOf(0, 0, 0, 0, 0, 0, 0)
        var ints = Array(7,{0})
        for (i in 1..6) {
//        ints[i - 1] = cofoxRandom(1, 34)
            var temp = cofoxRandom(1, 34)
            while (temp in ints){
                temp = cofoxRandom(1, 34)
            }
            ints[i - 1] = temp
        }
        //排序前六位，第七位当临时存储位
        for (i in ints.indices) {
            for (j in ints.indices - i) {
                if (j < ints.size - 1) {
                    if (ints[j] > ints[j + 1]) {
                        ints[ints.size - 1] = ints[j + 1]
                        ints[j + 1] = ints[j]
                        ints[j] = ints[ints.size - 1]
                    }
                }
            }
        }

        ints[ints.size-1] = cofoxRandom(1, 17)//蓝色号码球

        //按格式书写
        var str = "双色球："
        for (i in ints.indices) {
            if (ints[i].toString().length < 2){
                str += "0"+ints[i].toString()
            }else {
                str += ints[i]
            }

            if (i == ints.size-2){
                str += " —— "
            }else if (i == ints.size -1){

            }else{
                str += "  "
            }
        }
        ttvw.text = str
    }
    /**
     * 体彩大乐透随机猜想出号
     * */
    fun cofoxDaLeTou(ttvw:TextView){
//    var ints = intArrayOf(0, 0, 0, 0, 0, 0, 0)
        var ints = Array(7,{0})
        for (i in 1..5) {
//        ints[i - 1] = cofoxRandom(1, 36)
            var temp = cofoxRandom(1, 36)
            while (temp in ints){
                temp = cofoxRandom(1, 36)
            }
            ints[i - 1] = temp
        }

        //排序前5位，第6位当临时存储位
        for (i in ints.indices - 2) {
            for (j in ints.indices - i) {
                if (j < ints.size - 2) {
                    if (ints[j] > ints[j + 1]) {
                        ints[ints.size - 2] = ints[j + 1]
                        ints[j + 1] = ints[j]
                        ints[j] = ints[ints.size - 2]
                    }
                }
            }
        }

        ints[ints.size-2] = cofoxRandom(1, 13)//后区号码球1
        ints[ints.size-1] = cofoxRandom(1, 13)//后区号码球2

        //按格式书写
        var str = "大乐透："
        for (i in ints.indices) {
            if (ints[i].toString().length < 2){
                str += "0"+ints[i].toString()
            }else {
                str += ints[i]
            }

            if (i == ints.size-3){
                str += " ——"
            }else if (i == ints.size -1){

            }else{
                str += "  "
            }
        }
        ttvw.text = str
    }
}